package com.example.ch02.repository;

import com.example.ch02.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


public interface UserRepository extends JpaRepository<User,Long> {


   List<User> findByUser(String user);

   List<User> findByUserAndPwd(String user,String pwd);

   Long countByUser(String user);

   List<User> findByUserLike(String user);

   List<User> findUsersByUser (String user);


   @Query("select u from User u where u.pwd = ?1")//1是方法参数顺序
   public List<User> findByPwd(@Param("pwd") String pwd);

   @Query("select s from User s where s.userid=:userid")
   public List<User> findByUserid(@Param("userid") Long userid);


   @Transactional(timeout=10)
   @Modifying
   @Query("update User u set u.user=?1 where u.userid=?2")
   public int modifyUserById(String user,Long userid);

   @Query("select u from User u where u.id=?1")
   public Page<User> findPageByUserid(Long id, Pageable pageable);

}
